package com.neuedu.his.service;

import com.neuedu.his.dao.RoleDao;
import com.neuedu.his.dao.RoleDao;
import com.neuedu.his.dao.RoleMenuDao;
import com.neuedu.his.entity.Role;
import com.neuedu.his.entity.Role;
import com.neuedu.his.entity.RoleMenu;
import com.neuedu.his.entity.UserRole;
import com.neuedu.his.framework.BaseService;
import com.neuedu.his.framework.PageInfo;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 项目：      his
 * 类名:       RoleService
 * 创建时间：  2020/12/23  8:52
 * 描述 :     用户测试类
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class RoleService implements BaseService<Role> {
    private RoleDao roleDao = new RoleDao();
    private RoleMenuDao roleMenuDao = new RoleMenuDao();

    @Override
    public List<Role> queryList(HttpServletRequest request) {
        String sql = " select * from role where active = 1";
        return roleDao.selectList(sql);
    }


    /**
     * 分页查询
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @Override
    public PageInfo queryListByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        PageInfo pageInfo = getPageInfo(request);

        String roleName = request.getParameter("roleName");


        if(roleName == null ){
            roleName = "";
        }

        roleName = "%"+roleName.trim()+"%";


        String sql = " select * from role  where active = 1 and role_name like ?   limit ?  , ?  ";

        int start = (pageInfo.getPageNo()-1) * pageInfo.getPageSize() ;
        List<Role> list =  roleDao.selectList(sql,roleName, start,pageInfo.getPageSize());

        String sql2 = "  select count(1) from role    where active = 1 and role_name like ?   ";
        long count =  roleDao.selectCount(sql2,roleName);

        pageInfo.setList(list);
        pageInfo.setTotalCount((int) count);

        return pageInfo;
    }

    @Override
    public Role queryById(Integer id) {
        return roleDao.selectById(id);
    }

    @Override
    public boolean edit(Role user) {
        return false;
    }


    /**
     * 逻辑删除，没有真正删除数据
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {

        return  roleDao.deleteById(id);
    }

    @Override
    public boolean save(Role user) {
        return roleDao.insert(user);
    }

    @Override
    public boolean batchdel(String[] ids) throws Exception {
        return false;
    }

    public boolean update(Role user) {
        return roleDao.update(user);
    }

    public boolean grantRole(HttpServletRequest request) {

        String roleId = request.getParameter("roleId");
        String menuIdParms = request.getParameter("menuId");


        //维护role_menu表， 先删后插如
        String sql = "delete from role_menu where role_id = ?";
        roleMenuDao.delete(sql,roleId);

        boolean success = true;
        String[] menuIds = menuIdParms.split(",");
        for (String menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu(Integer.parseInt(roleId), Integer.parseInt(menuId));
            success = success && roleMenuDao.insert(roleMenu);
        }

        return success;
    }

    public List<RoleMenu> getRoleMenus(HttpServletRequest request) {
        String roleId = request.getParameter("roleId");
        return roleMenuDao.selectRoleMenu(Integer.parseInt(roleId));
    }
}
